<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']!='post'){
	if($_SESSION['os']!='windows'){
		exi('请在PC端打开！','error');
	}
	if(strpos($_RQ['op'],'juese') !== false){
		$this->juese_id=str_replace('juese','',$_RQ['op']);
		$_SESSION['juese_id']=$this->juese_id;
	}else{
		$this->juese_id=$_SESSION['juese_id'];
	}
	$return['pluginset']=$this->plugin_setting('basic');
	if($_SESSION['os']=='windows'){
		$return['session_id']=session_id();
	}
	$_SESSION['logout_forward']='jy_erp/login/';
	$_SESSION['forward_url']='jy_erp/login/'; 
	$return['forward_url']=base64_encode($_SESSION['forward_url']);
	$return['info']=array('juese_id'=>$this->juese_id);
	$return['user_juese']=$this->user_juese;
	$return['user_juesetitle']=$this->user_juese[$this->juese_id];
	if($_SESSION['uid']){
		unset($_SESSION['uid']);
		unset($_SESSION['storeid']);
		unset($_SESSION['member']);
		exi('','',custom_url('jy_erp/login/'.$_RQ['op'],true));
	}
	set_html('title','营销网+');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['password'] || !$post['username']){
		exi('请填写账户名和密码','error');
	}
	$tablename='jy_yxw_worker';
	$user=pdo_get($tablename,array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['username']));
	if(!$user || $post['password']!=base64_decode($user['password'])){
		exi('用户名或密码错误','error');
	}
	if(!$user['status']){
		exi('您的账号还未启用！','error');
	}
	if($user['status']==2){
		exi('您已离职，不能登录！','error');
	}
	if(!$user['mid']){
		do{
			$username='user_'.random(10);
		}while(pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$username),'id'));
		$member=array('username'=>$username,'password'=>trim($post['password']));
		$uid=model('core/member')->register($member);
		pdo_update($tablename,array('mid'=>$uid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		//exi('该机构没有绑定用户/管理员！','error');
	}
	if($user['storeid']){
		$store=pdo_get('jy_yxw_store',array('id'=>$user['storeid'],'uniacid'=>$_SESSION['uniacid']));
		$_SESSION['store_type']=$store['type'];
	}
	$_SESSION['toptitle']=$user['gangwei'];
	$_SESSION['uid']=$user['mid']?:$uid;
	$_SESSION['juese_id']=$user['juese'];
	$_SESSION['storeid']=$user['storeid'];
	$_SESSION['workerid']=$user['id'];
	$_SESSION['member']['username']=$user['username'];
	$_SESSION['member']['isonesys']=1;
	$_SESSION['istostore']=0;
	if(strpos($user['jueses'],';1;')!==false || $_SESSION['juese_id']==1){
		$_SESSION['isjuese1']=1;
	}else{
		unset($_SESSION['isjuese1']);
	}
	if(($_SESSION['juese_id']==14 || $_SESSION['isjuese1']) && $user['storeids']){
		$user['storeids']=json_decode($user['storeids'],true);
		$storeids=array();
		$storeids[]=$_SESSION['storeid'];
		foreach($user['storeids'] as $sv){
			if($sv['id']!=$_SESSION['storeid']){
				$storeids[]=$sv['id'];
			}
		}
		if(count($storeids)>1){
			$_SESSION['storeids']=$storeids;
		}
	}else{
		unset($_SESSION['storeids']);
	}
	$this->juese_id=$_SESSION['juese_id'];
	//绝对地址，会刷新页面
	if($this->juese_id==1 || $this->juese_id==2){
		$url='jy_erp/store.index/';
	}
	if($this->juese_id==14){
		$url='jy_erp/caiwu.infee//type=0';
	}
	if($this->juese_id==17){
		$url='jy_erp/caiwu.infee//type=0';
	}
	if($this->juese_id==20){
		$url='jy_erp/caigou.list/edit/';
	}
	if($this->juese_id==21){
		$url='jy_erp/caigou.list/edit/';
	}
	if($this->juese_id==22){
		$url='jy_erp/caigou.list/edit/';
	}
	exi('登陆成功!','',custom_url($url,true));
	
}